package orders;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;

/*
 * Abstract class implement Order class
 * Order items to list collection
 */
public abstract class AbstractOrder implements Order{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Collection<Item> lstItem = new Vector<Item>();
	private Date date = new Date();
	private double discount;
	private double totalPrice;
	
	@Override
	public Date getDate() {
		return date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public double getDiscount() {
		return discount;
	}

	@Override
	public void setDiscount(double discount) {
		this.discount = discount;
	}

	public AbstractOrder() {
		super();
		discount = 0;
		totalPrice = 0;
	}
	
	/*
	 * get all order in list lstItem to print to console:
	 * datetime, address, list item, totalPrice, Discount, TotalDiscount;
	 */
	@Override
	public final void getPrice() {
		DateFormat dateFormat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
		System.out.println("Date: " + dateFormat.format(date));
		
		System.out.println("Address: " + getAddress());
		System.out.println();
		
		Iterator<Item> items = lstItem.iterator();
		while(items.hasNext())
		{
			Item item = items.next();
			
			StringBuilder itemInfo = new StringBuilder();
			itemInfo.append(item.getName() + "\t");
			itemInfo.append(item.getPrice() + "x" + item.getNumber() + item.getUnit() + " ");

			double price = item.getNumber() * item.getPrice() - item.getNumber() * item.getPrice() * item.getDiscount() / 100 ;
			totalPrice += price;
			itemInfo.append(price);
			if (item.getDiscount() != 0)
			{
				itemInfo.append("(Discount " + item.getDiscount() + "%)");
			}
			
			System.out.println(itemInfo);
		}
		
		System.out.println();
		System.out.println("Total: " + totalPrice);
		System.out.println("Discount: " + discount + "%");
		System.out.println("TotalDiscount: " + (totalPrice - totalPrice * discount / 100));
		System.out.println("--------------------***--------------------");
	}
	
	/*
	 * add item to list item
	 * @see orders.Order#addItem(orders.Item)
	 */
	@Override
	public void addItem(Item item)
	{
		lstItem.add(item);
	}
	
	@Override
	public abstract String getAddress();
	
	/*
	 * compare date create Order with Order o
	 * return int
	 */
	@Override
	public int compareTo(Order o) {
		if (o == null)
		{
			return 0;
		}
		return (int) (this.getDate().getTime() - o.getDate().getTime());
	}
	
	/*
	 * get total price of this order
	 */
	@Override
	public double getTotalPrice()
	{
		return totalPrice;
	}
}
